package com.newtouch.cq.base.starter.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.newtouch.cq.base.starter.business.entity.SysRoleMenu;
import com.newtouch.cq.base.starter.business.mapper.BaseSysRoleMenuMapper;
import com.newtouch.cq.base.starter.business.model.dto.SysRoleMenuDTO;
import com.newtouch.cq.base.starter.business.service.IBaseSysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统角色和菜单的关系表 服务实现类
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@Service
public class BaseSysRoleMenuServiceImpl extends ServiceImpl<BaseSysRoleMenuMapper, SysRoleMenu> implements IBaseSysRoleMenuService {

    @Override
    public void deleteByRoleIdAndSave(SysRoleMenuDTO dto) {
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("role_id", dto.getRoleId());
        baseMapper.deleteByMap(map);

        List<SysRoleMenu> roleMenus = CollUtil.newArrayList();
        dto.getMenuIds().stream().forEach(menuId -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(dto.getRoleId());
            roleMenu.setMenuId(menuId);
            roleMenus.add(roleMenu);
        });

        this.saveBatch(roleMenus);
    }
}
